<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="demo">
        <input type="text" v-model="searchName">
        <ul>
            <li v-for="(person,index) in filterPersons" :key="person.id">
                {{index}}--{{person.id}}--{{person.name}}--{{person.age}}
            </li>
        </ul>
        <br>
        <button @click="sortType=1">年龄升序</button>
        <button @click="sortType=2">年龄降序</button>
        <button @click="sortType=0">恢复原来</button>
    </div>
    <script src="../day02/js/vue.js"></script>
    <script>
        new Vue({
            el:'#demo',
            data(){
                return{
                    searchName:'',//查询的字符串
                    sortType: 0,//0表示恢复默认排序，1表示升序排列，2表示降序排列
                    persons:[
                    {id: 1, name:"lingcong",age:27},
                    {id: 2, name:"yangzhi",age:24},
                    {id: 3, name:"lujingyi",age:30},
                    {id: 4, name:"yaogai",age:38},
                    {id: 5, name:"wuyong",age:34},
                    {id: 6, name:"luzhisheng",age:32},
                    {id: 7, name:"husanliang",age:19},
                    {id: 8, name:"mudachong",age:25},
                    {id: 9, name:"yanpoxi",age:22},
                    {id: 10, name:"panjinglian",age:18}
                    ]
                }
            },
            computed: {
                // 要显示的是过滤后的数组
                filterPersons(){
                    // 取出依赖数据
                    const {searchName,persons,sortType} = this;
                    // 计算结果
                    const arr = persons.filter((p)=>{
                        return p.name.includes(searchName);
                    })
                    // 可能需要排序（降序或者升序排列）
                    if(sortType!==0){
                        arr.sort((item1,item2)=>{
                            if(sortType===1){
                                return item1.age - item2.age;
                            }else{
                                return  item2.age - item1.age;
                            }
                        })
                    }
                    // 返回结果
                    return arr;
                }
            }
        })
    </script>
</body>
</html>